/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * 
     * @param S ListNode类 val表示权值，next指向下一个元素
     * @return ListNode类
     */
ListNode* solve(ListNode* S)
    {
        vector<int>v;
        //cout<<"S->val";
        while (S!=nullptr)
        {
            v.push_back(S->val);
            //cout<<S->val;
            S = S->next;
        }
        for(auto j:v){cout<<j<<" ";}
        int min = -999999;
        int min_i;
        for (int i = 0; i< v.size();i++)
        {
            if (v[i] <= min)
            {
                min = v[i];
                min_i = i;
            }
        }
        //
        ListNode* head;
        ListNode* p=head;
        for (int i = min_i; i < v.size(); i++)
        {
            p->val = v[i];
            p->next = new  ListNode(0);
            p = p->next;
        }
        for (int i = 0; i < min_i; i++)
        {
            p->val = v[i];
            
            if(i!=min_i-1)
            {
                p->next = new ListNode(0);
                p = p->next;
            }
            
        }
        return head;
    }
};